package com.fog.judge.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fog.judge.common.Base;
import com.fog.judge.common.UserContext;
import com.fog.judge.dto.ScoreDto;
import com.fog.judge.entity.QualitativeEvaluations;
import com.fog.judge.entity.Score;
import com.fog.judge.entity.Teacher;
import com.fog.judge.service.QualitativeEvaluationsService;
import com.fog.judge.service.ScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/score")
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @Resource
    private QualitativeEvaluationsService qualitativeEvaluationsService;

    @GetMapping("/page")
    public Base page(int page , int pageSize , String name){

        Page<Score> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Score> scoreQueryWrapper = new QueryWrapper<>();
        scoreQueryWrapper.orderByDesc("id");


        //teacherService.page(pageInfo, teacherQueryWrapper);
        return Base.success(pageInfo);
    }

    @GetMapping("/allscore")
    public Base score(){
        int userId = 1;/*UserContext.getUserId()*/;

        float Allscore = 0;
        float EvalutionScore_1 = 0;
        int EvalutionScore_1Num = 0;
        float EvalutionScore_2 = 0;
        int EvalutionScore_2Num = 0;
        List<QualitativeEvaluations> evaluations = qualitativeEvaluationsService.getQualitativeEvaluationsByTeacherId(userId);
        for (QualitativeEvaluations evaluation : evaluations) {
            if(evaluation.getEvalutionType()==1){
                double v = evaluation.getEvalutionScore() * 0.3;
                EvalutionScore_1+=v;
                EvalutionScore_1Num++;
            }else {
                double v = evaluation.getEvalutionScore() * 0.7;
                EvalutionScore_2+=v;
                EvalutionScore_2Num++;
            }
        }
        if (EvalutionScore_1Num > 0){
            Allscore += EvalutionScore_1/EvalutionScore_1Num;
        }
        if (EvalutionScore_2Num > 0){
            Allscore += EvalutionScore_2/EvalutionScore_2Num;
        }

        Score score = scoreService.findByTeacherId(userId);
        if (score!=null){
            // 修改总成绩
            Score score1 = new Score();
            score1.setId(score.getId());
            score1.setScore(Allscore);
            score1.setTeacherId(score.getTeacherId());
            scoreService.updateById(score1);
        }else {
            // 添加总成绩
            Score score1 = new Score();
            score1.setScore(Allscore);
            score1.setTeacherId(userId);
            scoreService.save(score1);
        }

        if(Allscore==0) {
            return Base.success("当前没有人评价");
        }else {
            return Base.success(Allscore);
        }
    }
}
